import Vue from 'vue'
import axios from 'axios'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import { router } from '../router'
import { getSession, clearSession } from '@/utils/session'
import { myMessage } from '../utils/resetMessage'

// axios.defaults.baseURL = "http://" + document.domain+":92/cqsz"
axios.defaults.baseURL = process.env.VUE_APP_BASEURL

axios.interceptors.request.use((config) => {
	nprogress.start()
	config.headers['Authorization'] = getSession('authorization')
	return config
})

axios.interceptors.response.use(
	(response) => {
		nprogress.done()
		let { code, msg } = response.data
		if (code == 0 || !code) {
			return response
		} else {
			if (code == 101) {
				//账号重复登录
				clearSession()
				router.replace({ name: 'login' })
				myMessage({ message: msg, type: 'warning' })
			} else if (msg == '用户身份验证失败') {
				console.log(router);
				router.replace({ name: 'login', query: { redirect: router.currentRoute.name } })
			} else {
				myMessage({ message: msg, type: 'warning' })
			}
		}
	},
	(err) => {
		return Promise.reject(err)
	},
)

export default axios